package com.ita.osc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.osc.bean.Cart;
import com.ita.osc.service.OrderManager;
import com.ita.osc.serviceImpl.OrderManagerImpl;
import com.ita.osc.util.JsonUtil;

/**
 * Servlet implementation class OrderDishServlet
 */
public class OrderDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrderManager orderManager = new OrderManagerImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderDishServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		HttpSession session = request.getSession();
		Cart cart = (Cart) session.getAttribute("cart");
		orderManager.checkoutCart(cart);
//		session.removeAttribute("cart");
		cart.getCartList().clear();
		session.setAttribute("cart", cart);
		PrintWriter writer = response.getWriter();
		writer.println(JsonUtil.objetc2Json("SUCCESS"));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
